package ania2.io;

//import ania2.Move;
//import ania2.Move;
import ania2.Move;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.*;

/**
 *
 * @author Grzegorz Brzeziński
 */
public class PdnNotationIo {



    public PdnNotationIo() {
        //this.movesTable = new java.util.LinkedList<Move>();
    }
//All of the following are optional and the sequence is unimportant, 
//but within a single [HEADER], each can appear a maximum of one time 
//only.
//
//[Event [EVENT_TEXT]][WHITESPACE]
//[Date [DATE_TEXT]][WHITESPACE]
//[Black [BLACK_PLAYER_TEXT]][WHITESPACE]
//[White [WHITE_PLAYER_TEXT]][WHITESPACE]
//[Site [SITE_TEXT]][WHITESPACE]
//[Result [RESULT_TEXT]][WHITESPACE]
//[Round [ROUND_TEXT]][WHITESPACE]
//[FEN [FEN_TEXT]][WHITESPACE]
//[ANNOTATION][WHITESPACE]
//
//The text from any of these headers must NOT include the following 
//characters: 
//
//" (double quote)
//[ (open square bracket)
//] (close square bracket)
//    GAME_BODY DEFINITION (Mandatory)
//
//The sequence of the following is fixed, apart from [ANNOTATION] and 
//[VARIATION], which may be interchanged.
//
//[
//[MOVE_SEQUENCE]		(Optional)
//[MOVE]			(Mandatory)
//[STRENGTH]			(Optional)
//[WHITESPACE]		(Optional)
//[ANNOTATION][WHITESPACE](Optional)
//[VARIATION][WHITESPACE]	(Optional)
//...]
//[WHITESPACE]		(Optional)
//[TERMINATOR]		(Mandatory)
//[WHITESPACE]		(Optional)
    public static String ToPdn(List<Move> movesTable,String event_text, String date_text, String black_player_text, String white_player_text, String site_text, String result_text, String round_text, String fen_text, String annotation) {
        String output = "";
        int licznik = 1;
        //String tmp="";

        output += event_text == null || event_text.length() == 0 ? "" : "[Event \"" + event_text + "\"] ";
// MUSI BYC W ODPOWIEDNIM FORMACIE YYYY-MM-DD!
        output += date_text == null || date_text.length() == 0 ? "" : "[Date \"" + date_text + "\"] ";
        output += black_player_text == null || black_player_text.length() == 0 ? "" : "[Black \"" + black_player_text + "\"] ";
        output += white_player_text == null || white_player_text.length() == 0 ? "" : "[White \"" + white_player_text + "\"] ";
        output += site_text == null || site_text.length() == 0 ? "" : "[Site \"" + site_text + "\"] ";
        output += result_text == null || result_text.length() == 0 ? "" : "[Result \"" + result_text + "\"] ";
        output += round_text == null || round_text.length() == 0 ? "" : "[Round \"" + round_text + "\"] ";
        output += annotation == null || annotation.length() == 0 ? "" : "{" + annotation + "} ";

// MUSI BYC W ODPOWIEDNIM FORMACIE! FEN <= pozycja startowa
        output += fen_text == null || fen_text.length() == 0 ? "" : "[Fen \"" + fen_text + "\"] ";

        for (int i = 0; i + 1 < movesTable.size(); i=i+2) {
            output += licznik + ". " + movesTable.get(i).getNotation() + " " + movesTable.get(i + 1).getNotation()+ " " ;
            licznik++;
        }

        return ConcatString(output, 80);
        
    }

    private  static String ConcatString(String input, int length) {
        int l = length;

        if (input.length() > l) {
            while (input.charAt(l) != ' ') {
                l--;
            }
            return input.substring(0, l) + System.getProperty("line.separator") + ConcatString(input.substring(l), length);
        } else {
            return input;
        }
    }

    public static void ReadPdn(String pdnInput) {
    Map<String,String> tempMap=new HashMap<String,String>();
    //String tmp[]=new String[];
    ArrayList tmp=new ArrayList<String>();
    
    pdnInput=pdnInput.replaceAll("[\r|\n]", "");
 //  String tmp="";
 //     System.out.println(pdnInput);
        
      
     Pattern p=Pattern.compile("([A-Za-z_0-9 ]+) \"([A-Za-z_0-9 ]+)\"");
     Matcher m =p.matcher(pdnInput);
     while(m.find()){
         tempMap.put(m.group(1), m.group(2));
     }
     System.out.println(tempMap);
     
     
     //p=p.compile("[A-Z|a-z][0-9]");
     m.reset();
     m.usePattern(Pattern.compile("[A-H|a-h][0-9][[-|x|X][A-H|a-h][0-9]]+"));
     
     //Move m=new Move();
     
     while(m.find()){
         tmp.add(m.group());
         //tmp.add(m.group(1));
         //tmp.add(m.group(2).substring(m.group(2).length()-1));
         //tmp+=m.group()+"/";
         //tmp+=m.group(1)+"->"+ m.group(2)+" ";
     }
 //    System.out.println(tmp);
        
    }



}
